package com.bstek.sample.service;

import com.bstek.bdf2.core.security.service.JwtTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @Title:  启动后执行方法
 * 注解方式@PostConstruct 始终最先执行
 *
 * 如果监听的是ApplicationStartedEvent 事件，则一定会在CommandLineRunner和ApplicationRunner 之前执行。
 *
 * 如果监听的是ApplicationReadyEvent 事件，则一定会在CommandLineRunner和ApplicationRunner 之后执行。
 *
 * CommandLineRunner和ApplicationRunner 默认是ApplicationRunner先执行，如果双方指定了@Order 则按照@Order的大小顺序执行，大的先执行。
 *
 *
 */

@Service
public class RuningService implements CommandLineRunner, ApplicationRunner
, ApplicationListener<ApplicationStartedEvent> {
    private static final Logger log = LoggerFactory.getLogger(RuningService.class);

    @PostConstruct
    public  void run(){
        log.info("PostConstruct....");
    }

    @Resource
    private JwtTokenService jwtTokenService;
    @Override
    public void run(String... args) throws Exception {
        log.info("CommandLineRunner....");
        String admin = jwtTokenService.createToken("admin");
        log.info("生成的token是:{}",admin);
        String token = jwtTokenService.parseToken(admin);
        log.info("解析的token是:{}",token);

    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("ApplicationRunner....");
    }

    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
        log.info("ApplicationStartedEvent....");
    }
}
